
package gui.mpm;

import clients.RPCClient;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.SplashScreen;
import java.awt.Toolkit;

import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import java.awt.SystemColor;


/**
 * @author Idan
 * GUI Frame class
 */
public class MPMFrame extends javax.swing.JFrame 
{
    public static RPCClient mpmClient = null;
    public MPMPanel mpmPanel;
    public MPMPlaning mpmPlaning;


	private static ImageIcon icon = new ImageIcon("/Hanit/src/graphics/bg.jpg");
	private static Image image = icon.getImage();

 
    /**
     * Creates new form MPMFrame
     */
    public MPMFrame()
    {
    	getContentPane().setBackground(new Color(173, 216, 230));
    	setIconImage(Toolkit.getDefaultToolkit().getImage(MPMFrame.class.getResource("/graphics/Spearhead.png")));
    	setTitle("ח.נ.י.ת");
        initComponents();
        this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
    }

    @SuppressWarnings("unchecked")
   
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGap(0, 1926, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGap(0, 1080, Short.MAX_VALUE)
        );
        getContentPane().setLayout(layout);

        pack();
    }


    public static void main(String args[])
    {
        
       
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
       
        /**
         * Create client and connect to the Server
         * */

        
            try
            {
               mpmClient = new RPCClient();
               if  (mpmClient== null)
            	   System.exit(0);
   

               // get list of orders
           }
           catch  (Exception e) 
           {
             //e.printStackTrace();
           }
           
        
        /* Create and display the form */
     
            SwingUtilities.invokeLater(new Runnable()
        {
            public void run() 
            {
            	
 	
            	MPMFrame mpmFrame = new MPMFrame();
                mpmFrame.mpmPanel = new MPMPanel(mpmFrame);
                mpmFrame.mpmPlaning = new MPMPlaning(mpmFrame);
                
                
                TitledBorder border = new TitledBorder(null, "ניהול פק''עות", TitledBorder.LEADING, TitledBorder.TOP, null, null);
                border.setTitleJustification(TitledBorder.RIGHT);
                mpmFrame.mpmPanel.setBorder(border);
//              
                MPMTabbedPanel tabbedPane = new MPMTabbedPanel(mpmFrame);
                mpmFrame.setContentPane(tabbedPane);
            	
            	
                // call functions
                ArrayList<Object> response = initializeAllFields();;
  
  

            
            if (response.get(0).toString().equalsIgnoreCase("initializeAllFieldsMPM"))
            {
            	if (response.get(0).toString().equalsIgnoreCase("Failed in initializeAllFieldsMPM") )
            	{
            		// Failed to get list
            		JOptionPane.showMessageDialog(mpmFrame,
						    "שגיאה בחיבור לשרת","שגיאה",JOptionPane.ERROR_MESSAGE);
      
            	}
            	else
            	{
            		
                    mpmFrame.mpmPanel.setVariables(response); 

                    mpmFrame.setVisible(true);    
            	}
            }
            else
            
         // Failed to get list
    		JOptionPane.showMessageDialog(mpmFrame,
				    "שגיאה בחיבור לשרת","שגיאה",JOptionPane.ERROR_MESSAGE);
                //set visible
            }
        });
    }

/**
 * Gets GUI data for all panels
 */

    private static ArrayList<Object> initializeAllFields()
    {
         ArrayList<Object> message = new ArrayList<Object>();
        message.add("initializeAllFieldsMPM");
         
        try 
        {
   
        	ArrayList<Object> response = mpmClient.call(message);
     
    
            return  response; 
        } catch (Exception ex) {
            Logger.getLogger(MPMFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        
       ArrayList<Object> failed = new ArrayList<Object>();
       failed.add("initializeAllFieldsMPM");
       failed.add("Failed in initializeAllFieldsMPM");
        
          return failed;
    }
    
    
}
    



